set
row/1*100000/
R17  region/
$include ..\define/region17.set
WLD
/
R  region/
$include ..\define/region_agmip.set
/
MAPR_AgMIP(R17,R)/
$include ..\define/region_agmip.map
/
SSP/SSP2,SSP3/
Srcp/RCP26,RCP85,nocc/
co2fert/co2,noco2,nocc/
returnpd                    /MAX,MIN,RP001,RP005,RP01,RP035,RP05,RP065,RP09,RP095,RP099/
returnpd_POPU(returnpd)     /RP05,RP065,RP09,RP095,RP099/
returnpd_CALO(returnpd)     /RP001,RP005,RP01,RP035,RP05/

SCENARIO /
SSP2_BaU_NoCC
SSP3_BaU_NoCC
SSP2_BaU_ge2m_RCP26_co2
SSP2_BaU_ge2m_RCP26_noco2
SSP2_BaU_ge2m_RCP85_co2
SSP2_BaU_ge2m_RCP85_noco2
SSP2_BaU_hg2e_RCP26_co2
SSP2_BaU_hg2e_RCP26_noco2
SSP2_BaU_hg2e_RCP85_co2
SSP2_BaU_hg2e_RCP85_noco2
SSP2_BaU_ipsl_RCP26_co2
SSP2_BaU_ipsl_RCP26_noco2
SSP2_BaU_ipsl_RCP85_co2
SSP2_BaU_ipsl_RCP85_noco2
SSP2_BaU_mes__RCP26_co2
SSP2_BaU_mes__RCP26_noco2
SSP2_BaU_mes__RCP85_co2
SSP2_BaU_mes__RCP85_noco2
SSP2_BaU_ne1m_RCP26_co2
SSP2_BaU_ne1m_RCP26_noco2
SSP2_BaU_ne1m_RCP85_co2
SSP2_BaU_ne1m_RCP85_noco2/
Y	/2020,2030,2040,2050/
A       /CALL,DUM/
VAR/CALO,POPU,VCAL,XPRP/

*VAR/PCAL,PPOP_UNSH,Proportion,Return/
VPCAL(VAR)/CALO/
VPPOP_UNSH(VAR)/POPU/
VVCAL(VAR)/VCAL/

Sgcm/ge2m,hg2e,ipsl,mes_,ne1m,nocc/
MAP_SCEN(SCENARIO,SSP,Sgcm,Srcp,co2fert)/
SSP2_BaU_ge2m_RCP26_co2	.	SSP2	.	ge2m	.	RCP26	.	co2
SSP2_BaU_ge2m_RCP26_noco2	.	SSP2	.	ge2m	.	RCP26	.	noco2
SSP2_BaU_ge2m_RCP85_co2	.	SSP2	.	ge2m	.	RCP85	.	co2
SSP2_BaU_ge2m_RCP85_noco2	.	SSP2	.	ge2m	.	RCP85	.	noco2
SSP2_BaU_hg2e_RCP26_co2	.	SSP2	.	hg2e	.	RCP26	.	co2
SSP2_BaU_hg2e_RCP26_noco2	.	SSP2	.	hg2e	.	RCP26	.	noco2
SSP2_BaU_hg2e_RCP85_co2	.	SSP2	.	hg2e	.	RCP85	.	co2
SSP2_BaU_hg2e_RCP85_noco2	.	SSP2	.	hg2e	.	RCP85	.	noco2
SSP2_BaU_ipsl_RCP26_co2	.	SSP2	.	ipsl	.	RCP26	.	co2
SSP2_BaU_ipsl_RCP26_noco2	.	SSP2	.	ipsl	.	RCP26	.	noco2
SSP2_BaU_ipsl_RCP85_co2	.	SSP2	.	ipsl	.	RCP85	.	co2
SSP2_BaU_ipsl_RCP85_noco2	.	SSP2	.	ipsl	.	RCP85	.	noco2
SSP2_BaU_mes__RCP26_co2	.	SSP2	.	mes_	.	RCP26	.	co2
SSP2_BaU_mes__RCP26_noco2	.	SSP2	.	mes_	.	RCP26	.	noco2
SSP2_BaU_mes__RCP85_co2	.	SSP2	.	mes_	.	RCP85	.	co2
SSP2_BaU_mes__RCP85_noco2	.	SSP2	.	mes_	.	RCP85	.	noco2
SSP2_BaU_ne1m_RCP26_co2	.	SSP2	.	ne1m	.	RCP26	.	co2
SSP2_BaU_ne1m_RCP26_noco2	.	SSP2	.	ne1m	.	RCP26	.	noco2
SSP2_BaU_ne1m_RCP85_co2	.	SSP2	.	ne1m	.	RCP85	.	co2
SSP2_BaU_ne1m_RCP85_noco2	.	SSP2	.	ne1m	.	RCP85	.	noco2
SSP2_BaU_NoCC	.	SSP2	.	nocc	.	nocc	.	nocc
SSP3_BaU_NoCC	.	SSP3	.	nocc	.	nocc	.	nocc
/
Ragg/
US
LAM
EU
China
SAsia
SEAsia
Afr_ME
FSU
Other
WLD
/
Rhunger(R)    flag for regions where hugner exists
MAP_Ragg(R,Ragg)/
*	WLD	.	WLD
	CAN	.	Other
	USA	.	US
	BRA	.	LAM
	CHN	.	China
	IND	.	SAsia
	OSA	.	LAM
	FSU	.	FSU
	EUR	.	EU
*	MEN	.	Afr_ME
*	SSA	.	Afr_ME
	SEA	.	SEAsia
	OAS	.	SAsia
	ANZ	.	Other
*	NAM	.	Other
*	OAM	.	LAM
	AME	.	Afr_ME
*	SAS	.
	CAN	.	WLD
	USA	.	WLD
	BRA	.	WLD
	CHN	.	WLD
	IND	.	WLD
	OSA	.	WLD
	FSU	.	WLD
	EUR	.	WLD
*	MEN	.	Afr_ME
*	SSA	.	Afr_ME
	SEA	.	WLD
	OAS	.	WLD
	ANZ	.	WLD
*	NAM	.	Other
*	OAM	.	LAM
	AME	.	WLD
*	SAS	.

$ontext
USA	.	US
XE25	.	EU
XER	.	EU
TUR	.	Other
XOC	.	Other
CHN	.	China
IND	.	SAsia
JPN	.	Other
XSE	.	SEAsia
XSA	.	SAsia
CAN	.	Other
BRA	.	LAM
XLM	.	LAM
CIS	.	FSU
XME	.	Afr_ME
XNF	.	Afr_ME
XAF	.	Afr_ME
USA	.	WLD
XE25	.	WLD
XER	.	WLD
TUR	.	WLD
XOC	.	WLD
CHN	.	WLD
IND	.	WLD
JPN	.	WLD
XSE	.	WLD
XSA	.	WLD
CAN	.	WLD
BRA	.	WLD
XLM	.	WLD
CIS	.	WLD
XME	.	WLD
XNF	.	WLD
XAF	.	WLD
$offtext
/
C_AGMIP/
*WHT,CGR,RIC,OSD,
AGR,ALL,CR5
/
Title_Agmip/CALO,POPU,POP,VCAL,XPRP/
Value/dum/
col/return/
Ausda/
"Wheat"
"Coarse graings"
"Corn"
"Rice"
"Cereal"
/
;


parameter
	foodshock(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col)      tonne per year
	foodshock_2050noco2(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col)      tonne per year
	foodshock_2050co2(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col)      tonne per year
	foodshock_2050SSP3(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col)      tonne per year
	PSDM(VAR,Y,R,Srcp,SSP,co2fert,returnpd)      standard deviation divided by mean
	PSDMRP(VAR,Y,R,Srcp,SSP,co2fert,returnpd)
	PSDM_nocc(VAR,Y,R,Srcp,SSP,co2fert,returnpd)      standard deviation divided by mean
	PSDMRP_nocc(VAR,Y,R,Srcp,SSP,co2fert,returnpd)

	VAR_RP0(VAR,Y,R,Srcp,SSP,co2fert,returnpd)	kcal per day per capita
        VAR_nocc(VAR,SSP,Y,R)    Per-capita calorie intake [kcal per person per day]

        POP(SSP,Y,R)               Population ratio of undernutrition [thausand]
	PRD_WHT(R)	wheat production in region R in 2005 [tonne per year]
	returnperiod(returnpd)	return period
        kcal_g(Ausda)	conversion factor from gramme to kcal [kcal per gramme]
	EJ_kcal
	MT_EJ_cereal
	g_kcal
;

kcal_g("Wheat")=535/183;
kcal_g("Coarse graings")=7/2.63;
kcal_g("Corn")=143/47.61;
kcal_g("Rice")=711/287.08;
kcal_g("Cereal")=(kcal_g("Wheat")+kcal_g("Rice")+kcal_g("Corn"))/3;
g_kcal=1/kcal_g("Cereal");

*MIN,RP001,RP002,RP005,RP01,RP02,RP033,RP05,RP066,RP08,RP09,RP095,RP098,RP099,MAX
returnperiod("RP001")=100;
returnperiod("RP005")=20;
returnperiod("RP01")=10;
returnperiod("RP035")=3;
returnperiod("RP05")=2;
returnperiod("RP065")=3;
returnperiod("RP09")=10;
returnperiod("RP095")=20;
returnperiod("RP099")=100;

EJ_kcal = 4184/10**18;

MT_EJ_cereal=1/(kcal_g("Cereal")*EJ_kcal*10**12);

PRD_WHT(R)=713182914;
$gdxin '../data/foodshockR.gdx'
$load foodshock
$gdxin '../data/foodshockR_2050noco2.gdx'
$load foodshock_2050noco2=foodshock
$gdxin '../data/foodshockR_2050co2.gdx'
$load foodshock_2050co2=foodshock
*$gdxin '../data/foodshockR_2050SSP3.gdx'
*$load foodshock_2050SSP3=foodshock

foodshock(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col)$foodshock_2050noco2(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col)=foodshock_2050noco2(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col);
foodshock(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col)$foodshock_2050co2(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col)=foodshock_2050co2(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col);
*foodshock(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col)$foodshock_2050SSP3(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col)=foodshock_2050SSP3(VAR,Y,R,Srcp,SSP,co2fert,returnpd,col);

*billion
VAR_RP0(VAR,Y,R,Srcp,SSP,co2fert,returnpd)=foodshock(VAR,Y,R,Srcp,SSP,co2fert,returnpd,"return");

*---Longterm
parameter
Pagmipout_longterm(Y,R,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip)
;

$gdxin '..\data\Pagmipout.gdx'
$load Pagmipout_longterm

POP(SSP,Y,R)$Pagmipout_longterm(Y,R,SSP,"nocc","nocc","nocc","ALL","POP")=Pagmipout_longterm(Y,R,SSP,"nocc","nocc","nocc","ALL","POP");
VAR_nocc("CALO",SSP,Y,R)$Pagmipout_longterm(Y,R,SSP,"nocc","nocc","nocc","AGR","CALO")=Pagmipout_longterm(Y,R,SSP,"nocc","nocc","nocc","AGR","CALO");
Rhunger(R)$(SUM((Y,SSP,Sgcm,Srcp,co2fert),Pagmipout_longterm(Y,R,SSP,Sgcm,Srcp,co2fert,"ALL","POPU")))=YES;

VAR_nocc("POPU",SSP,Y,R)$Pagmipout_longterm(Y,R,SSP,"nocc","nocc","nocc","AGR","POPU")=Pagmipout_longterm(Y,R,SSP,"nocc","nocc","nocc","AGR","POPU");
VAR_nocc("VCAL",SSP,Y,R)=Pagmipout_longterm(Y,R,SSP,"nocc","nocc","nocc","AGR","VCAL");
VAR_nocc("XPRP",SSP,Y,R)=Pagmipout_longterm(Y,R,SSP,"nocc","nocc","nocc","CR5","XPRP");

*---Longterm end

parameter
PCALOGAP(Y,R,Srcp,SSP,co2fert,returnpd)
PVCALGAP(Y,R,Srcp,SSP,co2fert,returnpd)
Pfoodstock(Y,R,Srcp,SSP,co2fert,returnpd)
PfoodstockVal(Y,R,Srcp,SSP,co2fert,returnpd)
PfoodstockRP(Y,R,Srcp,SSP,co2fert,returnpd)
PfoodstockRPVal(Y,R,Srcp,SSP,co2fert,returnpd)
Pfoodstock_agg(Y,Ragg,Srcp,SSP,co2fert,returnpd)
PfoodstockVal_agg(Y,Ragg,Srcp,SSP,co2fert,returnpd)
PfoodstockRP_agg(Y,Ragg,Srcp,SSP,co2fert,returnpd)
PfoodstockRPVal_agg(Y,Ragg,Srcp,SSP,co2fert,returnpd)
;


*PCALOGAP(Y,R,Srcp,SSP,co2fert,returnpd)$(returnpd_CALO(returnpd) and VAR_nocc("CALO",SSP,Y,R)-VAR_RP0("CALO",Y,R,Srcp,SSP,co2fert,returnpd)>0 and VAR_RP0("CALO",Y,R,Srcp,SSP,co2fert,returnpd))=VAR_nocc("CALO",SSP,Y,R)-VAR_RP0("CALO",Y,R,Srcp,SSP,co2fert,returnpd);
PCALOGAP(Y,R,Srcp,SSP,co2fert,returnpd)$(returnpd_CALO(returnpd) and VAR_RP0("CALO",Y,R,Srcp,SSP,co2fert,"RP05")-VAR_RP0("CALO",Y,R,Srcp,SSP,co2fert,returnpd)>0 and VAR_RP0("CALO",Y,R,Srcp,SSP,co2fert,returnpd))=VAR_RP0("CALO",Y,R,Srcp,SSP,co2fert,"RP05")-VAR_RP0("CALO",Y,R,Srcp,SSP,co2fert,returnpd);

Pfoodstock(Y,R,Srcp,SSP,co2fert,returnpd)$(returnpd_CALO(returnpd) and PCALOGAP(Y,R,Srcp,SSP,co2fert,returnpd)) = PCALOGAP(Y,R,Srcp,SSP,co2fert,returnpd) * POP(SSP,Y,R) * 10**3 * 365.2 * EJ_kcal * MT_EJ_cereal;
PfoodstockRP(Y,R,Srcp,SSP,co2fert,returnpd)$(Pfoodstock(Y,R,Srcp,SSP,co2fert,returnpd)) = returnperiod(returnpd);
Pfoodstock_agg(Y,Ragg,Srcp,SSP,co2fert,returnpd)=SUM(R$(MAP_Ragg(R,Ragg)),Pfoodstock(Y,R,Srcp,SSP,co2fert,returnpd));
PfoodstockRP_agg(Y,Ragg,Srcp,SSP,co2fert,returnpd)$(Pfoodstock_agg(Y,Ragg,Srcp,SSP,co2fert,returnpd)) = returnperiod(returnpd);

* USD/cap/day = kcal/cap/day * g_cerealeq/kcal * tonne/g * USD/t
PVCALGAP(Y,R,Srcp,SSP,co2fert,returnpd)=PCALOGAP(Y,R,Srcp,SSP,co2fert,returnpd)*g_kcal*10**(-6)*VAR_nocc("XPRP",SSP,Y,R) * 148;
*PVCALGAP(Y,R,Srcp,SSP,co2fert,returnpd)$(returnpd_CALO(returnpd) and VAR_nocc("VCAL",SSP,Y,R)-VAR_RP0("VCAL",Y,R,Srcp,SSP,co2fert,returnpd)>0 and VAR_RP0("VCAL",Y,R,Srcp,SSP,co2fert,returnpd))=VAR_nocc("VCAL",SSP,Y,R)-VAR_RP0("VCAL",Y,R,Srcp,SSP,co2fert,returnpd);

* billion USD = USD/cap/day  * people * day * 10**(-9)
PfoodstockVal(Y,R,Srcp,SSP,co2fert,returnpd)$(returnpd_CALO(returnpd) and PVCALGAP(Y,R,Srcp,SSP,co2fert,returnpd))= PVCALGAP(Y,R,Srcp,SSP,co2fert,returnpd) * POP(SSP,Y,R) * 10**3 * 365.2 * 10**(-9);
PfoodstockVal_agg(Y,Ragg,Srcp,SSP,co2fert,returnpd)=SUM(R$(MAP_Ragg(R,Ragg)),PfoodstockVal(Y,R,Srcp,SSP,co2fert,returnpd));

PfoodstockRPVal(Y,R,Srcp,SSP,co2fert,returnpd)$(PfoodstockVal(Y,R,Srcp,SSP,co2fert,returnpd)) = returnperiod(returnpd);
PfoodstockRPVal_agg(Y,Ragg,Srcp,SSP,co2fert,returnpd)$(PfoodstockVal_agg(Y,Ragg,Srcp,SSP,co2fert,returnpd)) = returnperiod(returnpd);

*-- Regional aggregation

PSDM(VAR,Y,R,Srcp,SSP,co2fert,returnpd)$(VAR_RP0(VAR,Y,R,Srcp,SSP,co2fert,returnpd) and VAR_RP0(VAR,Y,R,Srcp,SSP,co2fert,"RP05")) =(VAR_RP0(VAR,Y,R,Srcp,SSP,co2fert,returnpd)/VAR_RP0(VAR,Y,R,Srcp,SSP,co2fert,"RP05")-1)*100;
* MAX,MIN is not included in return period!!
*PSDMRP(VAR,Y,R,Srcp,SSP,co2fert,returnpd)$(PSDM(VAR,Y,R,Srcp,SSP,co2fert,returnpd)) = returnperiod(returnpd);

PSDM_nocc(VAR,Y,R,Srcp,SSP,co2fert,returnpd)$(VAR_RP0(VAR,Y,R,Srcp,SSP,co2fert,returnpd) and VAR_nocc(VAR,SSP,Y,R))=(VAR_RP0(VAR,Y,R,Srcp,SSP,co2fert,returnpd)/VAR_nocc(VAR,SSP,Y,R)-1)*100;
* MAX,MIN is not included in return period!!
*PSDMRP_nocc(VAR,Y,R,Srcp,SSP,co2fert,returnpd)$(PSDM_nocc(VAR,Y,R,Srcp,SSP,co2fert,returnpd)) = returnperiod(returnpd);
*-- Regional aggregation end


set
Rusda/
$include ..\define/region_usda.set
/
MAP_Rusda(Rusda,Ragg)/
$include ..\define/region_usda.map
/
;
parameter
ActualStock0(Rusda,Ausda) [Mt]
ActualStock(Ragg)
;

$libinclude xlimport ActualStock0 ..\data\Actualstock_USDA.xlsx

ActualStock(Ragg)=SUM(Ausda,SUM(Rusda$MAP_Rusda(Rusda,Ragg),ActualStock0(Rusda,Ausda)*10**12*kcal_g(Ausda)*EJ_kcal))*MT_EJ_cereal;
ActualStock("Other")=ActualStock("WLD")-SUM(Ragg$((not sameas(Ragg,"WLD")) and (not sameas(Ragg,"Other")) ),ActualStock(Ragg));



Execute_unload '..\output\gdx\Pfoodstock.gdx'
VAR_RP0
PCALOGAP
PVCALGAP
*Pfoodstock
Pfoodstock_agg
*PfoodstockRP
PfoodstockRP_agg
*PfoodstockVal
PfoodstockVal_agg
PfoodstockRPVal_agg
ActualStock
PSDM
*PSDMRP
PSDM_nocc
*PSDMRP_nocc

POP
VAR_nocc
foodshock
Rhunger
kcal_g
MT_EJ_cereal
;


;
